package com.xm.xmapp;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.xm.xmapp.app.Constants;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.ShareDialog;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.myinterface.MyDialogButton;
import com.xm.xmapp.request.bean.Invite_register_configRequest;
import com.xm.xmapp.response.bean.ResponseInviteRegisterConfig;
import com.xm.xmapp.response.bean.ResponseUserBean;
import com.xm.xmapp.tools.BeanTools;
import com.xm.xmapp.tools.Tools;

public class MyShareActivity extends BaseActivity implements OnClickListener {

	private ImageView img4, img2;
	private TextView text;
	private String str_title = "Hey朋友！塑造文明城市，点亮绿色生活需要你我一起努力";
	private String str_content = "快快点我注册，20元代金券请接住，你我一起共享绿色生活。";
	private String url, sign;
	private UMImage image = new UMImage(MyAppcation.getMyAppcation(), R.mipmap.guanyu_icon);
	private UMWeb web;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_myshare);
	}

	@Override
	public void initView() {
		img4 = (ImageView) findViewById(R.id.img4);
		img2 = (ImageView) findViewById(R.id.img2);
		text = (TextView) findViewById(R.id.text);
	}

	@Override
	public void initData() {

		setMyTitle("邀请好友");
		Invite_register_configRequest requestBean = new Invite_register_configRequest();
		ResponseUserBean bean = MyAppcation.getMyAppcation().getUserBean();
		if (bean != null) {
			sign = "uid=" + bean.user_id + "&phone=" + bean.user_phone + "&" + Constants.certkey;
			sign = BeanTools.md5(sign);
			url = Constants.shareUrl + "uid=" + bean.user_id + "&phone=" + bean.user_phone + "&sign=" + sign;
			requestBean.phone = bean.user_phone;
		}

		getData(requestBean);
		String str_text = "邀请您的好友加入一步用车送给朋友20元\n 用车优惠券，当TA审核通过时\n 您将获得20元优惠券。";
		int bstart = str_text.indexOf("朋友") + 2;
		int fstart = str_text.indexOf("获得") + 2;
		SpannableStringBuilder style = new SpannableStringBuilder(str_text);
		style.setSpan(new ForegroundColorSpan(Color.RED), bstart, bstart + 2, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
		style.setSpan(new ForegroundColorSpan(Color.RED), fstart, fstart + 2, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
		text.setText(style);

	}

	private void getData(Invite_register_configRequest requestBean) {

		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				promptDialog.dismiss();
				ResponseInviteRegisterConfig base = JSON.parseObject(response, ResponseInviteRegisterConfig.class);
				if (!base.share_image.equals(""))
					image = new UMImage(MyAppcation.getMyAppcation(), base.share_image);
				if (!base.share_url.equals(""))
					url = base.share_url;
				if (!base.share_title.equals(""))
					str_title = base.share_title;
				if (!base.share_content.equals(""))
					str_content = base.share_content;
				if (base.share_show_content != null && !base.share_show_content.equals(""))
					text.setText(base.share_show_content);
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});

	}

	@Override
	public void initEvent() {
		img4.setOnClickListener(this);
		img2.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img2:
			intent = new Intent(MyAppcation.getMyAppcation(), ShowURLActivity.class);
			intent.putExtra("url", Constants.shareGuizeUrl);
			intent.putExtra("title", "活动规则");
			startActivity(intent);
			break;
		case R.id.img4:
			share();
			break;
		default:
			break;
		}

	}

	private void share() {
		ShareDialog shareDialog = new ShareDialog(MyShareActivity.this, new MyDialogButton() {

			@Override
			public void setTitle(int flag, String str) {

				web = new UMWeb(url);
				web.setTitle(str_title);
				web.setThumb(image);
				web.setDescription(str_content);

				UMShareListener listener = new UMShareListener() {

					@Override
					public void onResult(SHARE_MEDIA arg0) {
						Tools.showMessage("分享成功");
					}

					@Override
					public void onError(SHARE_MEDIA arg0, Throwable arg1) {

						Tools.showMessage("分享失败");
					}

					@Override
					public void onCancel(SHARE_MEDIA arg0) {

						Tools.showMessage("用户取消");
					}

					@Override
					public void onStart(SHARE_MEDIA arg0) {
					}
				};

				switch (flag) {
				case 0:

					new ShareAction(MyShareActivity.this).withText(str_content).withMedia(web)
							.setPlatform(SHARE_MEDIA.QQ).setCallback(listener).share();

					// new ShareAction(MyShareActivity.this)
					// .setPlatform(SHARE_MEDIA.QQ)
					// .withTitle(str_title).withText(str_content)
					// .withMedia(image).withTargetUrl(url)
					// .setCallback(listener).share();
					break;
				case 1:

					new ShareAction(MyShareActivity.this).withText(str_content).withMedia(web)
							.setPlatform(SHARE_MEDIA.QZONE).setCallback(listener).share();
					// new ShareAction(MyShareActivity.this)
					// .setPlatform(SHARE_MEDIA.QZONE)
					// .withTitle(str_title).withText(str_content)
					// .withMedia(image).withTargetUrl(url)
					// .setCallback(listener).share();
					break;
				case 2:
					// Config.isUmengSina = true;
					// http://sns.whalecloud.com/sina2/callback
					// Config.REDIRECT_URL =
					// "http://sns.whalecloud.com/sina2/callback";

					new ShareAction(MyShareActivity.this).withText(str_content).withMedia(web)
							.setPlatform(SHARE_MEDIA.SINA).setCallback(listener).share();

					// new ShareAction(MyShareActivity.this)
					// .setPlatform(SHARE_MEDIA.SINA)
					// .withTitle(str_title).withText(str_content)
					// .withMedia(image).withTargetUrl(url)
					// .setCallback(listener).share();
					break;
				case 3:

					new ShareAction(MyShareActivity.this).withText(str_content).withMedia(web)
							.setPlatform(SHARE_MEDIA.WEIXIN).setCallback(listener).share();
					// new ShareAction(MyShareActivity.this)
					// .setPlatform(SHARE_MEDIA.WEIXIN)
					// .withTitle(str_title).withText(str_content)
					// .withMedia(image).withTargetUrl(url)
					// .setCallback(listener).share();
					break;

				case 4:
					new ShareAction(MyShareActivity.this).withText(str_content).withMedia(web)
							.setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).setCallback(listener).share();
					// new ShareAction(MyShareActivity.this)
					// .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)
					// .withTitle(str_title).withText(str_content)
					// .withMedia(image).withTargetUrl(url)
					// .setCallback(listener).share();
					break;

				default:
					break;
				}
			}
		});
		shareDialog.show();

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		UMShareAPI.get(this).release();
	}

}
